using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Instedd.Labs.Forms.Library;
using System.Collections.Generic;
using System.Xml;

public partial class CasesByArea : System.Web.UI.Page
{
    Dictionary<Area, List<Survey>> areas = new Dictionary<Area, List<Survey>>();


    List<Area> ReadAreasFromKml()
    {
        List<Area> areas = new List<Area>();
        Polygon poly;
        poly = PolygonBuilder.FromKMLString("-122.2090215617799,37.46734643323851,0 -122.1957058470803,37.45989738457819,0 -122.18956426726,37.46748112842352,0 -122.201141181,37.4737499349141,0 -122.2090215617799,37.46734643323851,0");
        areas.Add(new Area("Lloyden Park Neighborhood", poly));

        poly = PolygonBuilder.FromKMLString("-122.1371489331245,37.47370096042816,0 -122.137045386087,37.47174604687308,0 -122.1348979876701,37.47184333642391,0 -122.13516671561,37.4722255358379,0 -122.1352790422689,37.47370118665313,0 -122.1371489331245,37.47370096042816,0");
        areas.Add(new Area("Ravenswood Team-1", poly));

        poly = PolygonBuilder.FromKMLString("-122.1957012931335,37.45970317131792,0 -122.20395346231,37.45407527270083,0 -122.203518569254,37.44801824886269,0 -122.1947295485664,37.44498552861711,0 -122.1889270942269,37.4488298472756,0 -122.1865511337915,37.45388104599215,0 -122.187415464501,37.45576690676438,0 -122.1957012931335,37.45970317131792,0");
        areas.Add(new Area("Sacred Heart School", poly));

        poly = PolygonBuilder.FromKMLString("-122.1265706621593,37.46698923155096,0 -122.1392486852369,37.46691582329033,0 -122.1405649211099,37.4624371725817,0 -122.140329169578,37.46036842771339,0 -122.1382447498928,37.4592893837092,0 -122.1372275308102,37.45864426676471,0 -122.1328087773311,37.45620899208662,0 -122.1273641007909,37.45320739942616,0 -122.1242936770814,37.45261709060846,0 -122.1218947246958,37.45323199592919,0 -122.1218506035681,37.45439512586172,0 -122.1223325328475,37.45703694096441,0 -122.1249288846988,37.46391550790115,0 -122.1265706621593,37.46698923155096,0");
        areas.Add(new Area("Gardens and Palo Mobile", poly));

        poly = PolygonBuilder.FromKMLString("-122.138123,37.468491,0 -122.13813,37.467747,0 -122.136337,37.467747,0 -122.136375,37.468548,0 -122.138123,37.468491,0");
        areas.Add(new Area("Ravenswood Team-2", poly));

        //areas.Add(new Area("Unknown",null));

        return areas;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        List<Area> areas = ReadAreasFromKml();

        this.Repeater1.DataSource = areas;

        List<Survey> surveys = SurveyManager.GetInstance().GetAllSurveys();
        Area unknownArea = new Area("Unknown", null);

        foreach (Survey s in surveys)
        {
            bool didHit = false;

            foreach (Area area in areas)
            {
                if (area.Polygon.IsContained(new PPoint(GetDoubleValue(s.Longitude), GetDoubleValue(s.Latitude))))
                {
                    didHit = true;
                    area.Surveys.Add(s);
                    break;
                }
            }

            if (!didHit)
            {
                unknownArea.Surveys.Add(s);
            }
        }

        areas.Add(unknownArea);

        this.Repeater1.DataBind();
    }

    double GetDoubleValue(double? coord)
    {
        if (coord == null)
        {
            return 0;
        }
        return (double)coord;
    }
    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        GridView gridView = e.Item.FindControl("SurveysGridView") as GridView;
        Area area = e.Item.DataItem as Area;

        gridView.DataSource = area.Surveys;
        gridView.DataBind();
    }
}